import { Component, OnInit, Input, Output, EventEmitter, forwardRef } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { LoveComponentsService } from '../love-components.service';

@Component({
  selector: 'makeup-setup-modal',
  templateUrl: './makeup-setup-modal.component.html',
  styleUrls: ['./makeup-setup-modal.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => MakeupSetupModalComponent),
      multi: true
    }
  ]
})
export class MakeupSetupModalComponent implements ControlValueAccessor, OnInit {

  // @Input() checkedVal: string | number;
  @Input() otherParams: any = {};
  // @Input() orderId!: string | number;
  @Input() setupDisabled: boolean = false;
  @Output() submitSetup = new EventEmitter();

  isMakeVisible = false;
  setupLoading = false;
  _onChange: any = () => {};

  radioValue = '';
  isSpinning = false;
  modalData: any = {}; // { frontPromotionInfoList, frontMarkUpRecycleCouponInfoVOList, afterPromotionInfoList, afterMarkUpRecycleCouponInfoVOList }

  constructor(
    private message: NzMessageService,
    private services: LoveComponentsService
  ) { }

  ngOnInit(): void {}

  async loadMakeData() {
    this.isSpinning = true;
    const {code, data, message}: any = await this.services.queryMakeupSetupData({id: this.otherParams.id});
    this.isSpinning = false;

    if (code === 0) {
      this.modalData = data || {};

      this.radioValue = data.optimal || '';
    } else {
      this.message.error(message);
    }
  }

  handleCancel() {
    this.isMakeVisible = false;
    this._onChange(false);
  }
  
  async submitCoupon() {
    if (this.isEmpty(this.radioValue)) {
      this.message.warning('请先选择活动或加价券');
      return
    }
    let tempArrs: any[] = [];
    for (let key in this.modalData) {
      if (Object.prototype.toString.call(this.modalData[key]) === '[object Array]') {
        tempArrs = tempArrs.concat(this.modalData[key]);
      }
    }
    let tempObj = tempArrs.find((item: any) => item.radioValue === this.radioValue);
    console.log('obobobob', tempObj);

    let params = {
      ...this.otherParams,
      // id: this.orderId, // 订单ID
      // userId: '',
      type: tempObj.radioType, // 0加价券  1加价活动
      couponUserRecordId: tempObj.couponUserRecordId || '' , // 用户加价ID
      couponNo: tempObj.no || '', // 加价券码
      promotionId: tempObj.radioType === 1 ? tempObj.id : '' // 加价活动ID
    }
    const {code, data, message}: any = await this.services.updateMakeupSetupData(params);
    if (code === 0) {
      this.message.success('加价设置成功');
      this.handleCancel();
      this.submitSetup.emit(params);
    } else {
      this.message.error(message);
    }
  }

  writeValue(val: any) {
    if (this.isEmpty(val)) {
      return
    }

    if (val) {
      this.loadMakeData();
    }
    this.isMakeVisible = val;
  }

  registerOnChange(fn: (_: any) => void): void {
    this._onChange = fn;
  }

  registerOnTouched(fn: any): void {}

  isEmpty(val: any) {
    if (val === '' || val === undefined || val === null) {
      return true;
    } else {
      return false;
    }
  }

  returnMakeupPrice(item: any) {
    let sum = 0;
    sum = (item?.orderLimitMoney || 0) * (item.limitSmall / 100);
    return item.type === 1 ? item.limitSmall : sum;
  }
}
